package com.ruoyi.web.controller.test;


import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


import java.io.*;
import java.util.*;

@RestController
@RequestMapping("/system/testUser")
public class WriteExcelTableController {

    private static final String UPLOAD_URL = "http://192.168.110.183:8012/fileView/fileUpload";
    private static final String TEMPLATE_FILE_NAME = "template.xls";

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/generateWordTable")
    public Object generateWordTable() {
        //创建一个excel
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("工作表1");//创建一个excel的sheet

        String filename = new Date().getTime() + ".xls";// 设置下载时客户端Excel的名称
        //设置居中样式
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//设置垂直居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//设置水平居中
        cellStyle.setBorderBottom(BorderStyle.THIN); // 下边框
        cellStyle.setBorderLeft(BorderStyle.THIN);// 左边框
        cellStyle.setBorderTop(BorderStyle.THIN);// 上边框
        cellStyle.setBorderRight(BorderStyle.THIN);// 右边框

        //设置1,2列列宽  共 7 列
        int colSum = 7;
        sheet.setColumnWidth(0, 11 * 256);
        sheet.setColumnWidth(1, 12 * 256);
        sheet.setColumnWidth(2, 11 * 256);
        sheet.setColumnWidth(3, 11 * 256);
        sheet.setColumnWidth(4, 11 * 256);
        sheet.setColumnWidth(5, 12 * 256);
        sheet.setColumnWidth(6, 11 * 256);

        //设置背景色样式
        HSSFCellStyle backgroundStyle = workbook.createCellStyle();
        //设置填充样式
        backgroundStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        //设置背景色
        backgroundStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex());
        backgroundStyle.setBorderBottom(BorderStyle.THIN); // 下边框
        backgroundStyle.setBorderLeft(BorderStyle.THIN);// 左边框
        backgroundStyle.setBorderTop(BorderStyle.THIN);// 上边框
        backgroundStyle.setBorderRight(BorderStyle.THIN);// 右边框

        int firstRowNumber = sheet.getLastRowNum() + 1;
        HSSFRow firstRow1 = sheet.createRow(firstRowNumber);
        for (int i = 0; i < colSum; i++) {
            firstRow1.createCell(i);
        }
        firstRow1.getCell(0).setCellValue("产品名称");
        firstRow1.getCell(1).setCellValue("？");
        firstRow1.getCell(5).setCellValue("制定人");
        firstRow1.getCell(6).setCellValue("？");


        int secondRowNumber = sheet.getLastRowNum() + 1;
        HSSFRow secondRow2 = sheet.createRow(secondRowNumber);
        for (int i = 0; i < colSum; i++) {
            secondRow2.createCell(i);
        }
        secondRow2.getCell(0).setCellValue("规格型号");
        secondRow2.getCell(1).setCellValue("？");
        secondRow2.getCell(5).setCellValue("审核人");
        secondRow2.getCell(6).setCellValue("？");


        int thirdRowNumber = sheet.getLastRowNum() + 1;

        HSSFRow thirdRow3 = sheet.createRow(thirdRowNumber);
        for (int i = 0; i < colSum; i++) {
            thirdRow3.createCell(i);
        }
        thirdRow3.getCell(0).setCellValue("工序名称");
        thirdRow3.getCell(1).setCellValue("？");
        thirdRow3.getCell(5).setCellValue("标准");
        thirdRow3.getCell(6).setCellValue("？");
        //第一行1,2列  合并单元格
        sheet.addMergedRegion(new CellRangeAddress(firstRowNumber, thirdRowNumber, 2, 4));
        //第一行一二列合并单元格后添加数据
        firstRow1.getCell(2).setCellValue("生产工艺卡");

//      物料辅料行
        int materialTitleNumber = sheet.getLastRowNum() + 1;
        HSSFRow materialTitleRow = sheet.createRow(materialTitleNumber);
        for (int i = 0; i < colSum; i++) {
            materialTitleRow.createCell(i);
        }
        sheet.addMergedRegion(new CellRangeAddress(materialTitleNumber, materialTitleNumber, 0, 4));
        sheet.addMergedRegion(new CellRangeAddress(materialTitleNumber, materialTitleNumber, 5, 6));
        materialTitleRow.getCell(0).setCellValue("物料");
        materialTitleRow.getCell(5).setCellValue("辅料");

        int materialNameTypeNumber = sheet.getLastRowNum() + 1;

        HSSFRow materialNameTypeRow4 = sheet.createRow(materialNameTypeNumber);
        for (int i = 0; i < colSum; i++) {
            materialNameTypeRow4.createCell(i);
        }
        sheet.addMergedRegion(new CellRangeAddress(materialNameTypeNumber, materialNameTypeNumber, 1, 2));
        sheet.addMergedRegion(new CellRangeAddress(materialNameTypeNumber, materialNameTypeNumber, 3, 4));
        materialNameTypeRow4.getCell(0).setCellValue("NO.");
        materialNameTypeRow4.getCell(1).setCellValue("物料");
        materialNameTypeRow4.getCell(3).setCellValue("规格");
        materialNameTypeRow4.getCell(5).setCellValue("NO.");
        materialNameTypeRow4.getCell(6).setCellValue("辅料");


//      物料
        List<Map<String, Object>> studentList = new ArrayList<>();
        Map<String, Object> stu1 = new HashMap<>();
        stu1.put("name", "张无忌");
        stu1.put("sex", "男");
        Map<String, Object> stu2 = new HashMap<>();
        stu2.put("name", "赵敏");
        stu2.put("sex", "女");
        Map<String, Object> stu3 = new HashMap<>();
        stu3.put("name", "周芷若");
        stu3.put("sex", "女");
        Map<String, Object> stu4 = new HashMap<>();
        stu4.put("name", "金毛狮王");
        stu4.put("sex", "男");
        Map<String, Object> stu5 = new HashMap<>();
        stu5.put("name", "殷素素");
        stu5.put("sex", "女");
        studentList.add(stu1);
        studentList.add(stu2);
        studentList.add(stu3);
        studentList.add(stu4);
        studentList.add(stu5);


        int materialRowNum = sheet.getLastRowNum() + 1;
        for (int i = 0; i < studentList.size(); i++) {
            int index = i + materialRowNum;
            HSSFRow rowi = sheet.createRow(index);
            for (int t = 0; t < 5; t++) {
                rowi.createCell(t);
            }
            sheet.addMergedRegion(new CellRangeAddress(index, index, 1, 2));
            sheet.addMergedRegion(new CellRangeAddress(index, index, 3, 4));
            rowi.getCell(0).setCellValue(String.valueOf(i + 1));
            rowi.getCell(1).setCellValue(studentList.get(i).get("name").toString());
            rowi.getCell(3).setCellValue(studentList.get(i).get("sex").toString());
        }
//      辅料
        List<Map<String, Object>> studentList11 = new ArrayList<>();
        Map<String, Object> stu111 = new HashMap<>();
        stu111.put("name", "张无忌11");
        Map<String, Object> stu211 = new HashMap<>();
        stu211.put("name", "赵敏11");
        Map<String, Object> stu311 = new HashMap<>();
        stu311.put("name", "周芷若11");
        Map<String, Object> stu411 = new HashMap<>();
        stu411.put("name", "金毛狮王11");
        Map<String, Object> stu511 = new HashMap<>();
        stu511.put("name", "殷素素11");
        Map<String, Object> stu611 = new HashMap<>();
        stu611.put("name", "殷素素22");
        studentList11.add(stu111);
        studentList11.add(stu211);
        studentList11.add(stu311);
        studentList11.add(stu411);
        studentList11.add(stu511);
        studentList11.add(stu611);
        for (int i = 0; i < studentList11.size(); i++) {
            int index = i + materialRowNum;
            HSSFRow rowi = sheet.getRow(index);
            if (Objects.isNull(rowi)) {
//              第二个数据列超出左边的模块时需要初始化左边的模块
                sheet.addMergedRegion(new CellRangeAddress(index, index, 1, 2));
                sheet.addMergedRegion(new CellRangeAddress(index, index, 3, 4));
                rowi = sheet.createRow(index);
                for (int t = 0; t < 5; t++) {
                    rowi.createCell(t);
                }
                rowi.getCell(0).setCellValue(String.valueOf(i + 1));
            }
            for (int t = 5; t < 7; t++) {
                rowi.createCell(t);
            }
            rowi.getCell(5).setCellValue(String.valueOf(i + 1));
            rowi.getCell(6).setCellValue(studentList11.get(i).get("name").toString());
        }
        int deviceRowNumber = sheet.getLastRowNum() + 1;
        HSSFRow rowDevice = sheet.createRow(deviceRowNumber);
        for (int t = 0; t < colSum; t++) {
            rowDevice.createCell(t);
        }
        sheet.addMergedRegion(new CellRangeAddress(deviceRowNumber, deviceRowNumber, 0, 6));
        rowDevice.getCell(0).setCellValue("工装及设备");

//      工装设备及标题
        int deviceTitleRowNumber = sheet.getLastRowNum() + 1;
        HSSFRow deviceTitleRow = sheet.createRow(deviceTitleRowNumber);
        for (int t = 0; t < colSum; t++) {
            deviceTitleRow.createCell(t);
        }

        sheet.addMergedRegion(new CellRangeAddress(deviceTitleRowNumber, deviceTitleRowNumber, 1, 3));
        sheet.addMergedRegion(new CellRangeAddress(deviceTitleRowNumber, deviceTitleRowNumber, 4, 6));
        deviceTitleRow.getCell(0).setCellValue("NO.");
        deviceTitleRow.getCell(1).setCellValue("名称");
        deviceTitleRow.getCell(4).setCellValue("规格");


        int deviceCountRowNumber = sheet.getLastRowNum() + 1;
//      辅料
        List<Map<String, Object>> deviceList11 = new ArrayList<>();
        Map<String, Object> device111 = new HashMap<>();
        device111.put("name", "device611张无忌11");
        device111.put("sex", "device111");
        Map<String, Object> device211 = new HashMap<>();
        device211.put("name", "device611赵敏11");
        device211.put("sex", "device211");
        Map<String, Object> device311 = new HashMap<>();
        device311.put("name", "device611周芷若11");
        device311.put("sex", "device311");
        Map<String, Object> device411 = new HashMap<>();
        device411.put("name", "device611金毛狮王11");
        device411.put("sex", "device411");
        Map<String, Object> device511 = new HashMap<>();
        device511.put("name", "device611殷素素11");
        device511.put("sex", "device511");
        Map<String, Object> device611 = new HashMap<>();
        device611.put("name", "device611殷素素22");
        device611.put("sex", "device611");

        deviceList11.add(device111);
        deviceList11.add(device211);
        deviceList11.add(device311);
        deviceList11.add(device411);
        deviceList11.add(device511);
        deviceList11.add(device611);

        for (int i = 0; i < deviceList11.size(); i++) {
            int index = i + deviceCountRowNumber;
            HSSFRow rowi = sheet.createRow(index);
            sheet.addMergedRegion(new CellRangeAddress(index, index, 1, 3));
            sheet.addMergedRegion(new CellRangeAddress(index, index, 4, 6));
            for (int t = 0; t < colSum; t++) {
                rowi.createCell(t);
            }
            rowi.getCell(0).setCellValue(String.valueOf(i + 1));
            rowi.getCell(1).setCellValue(deviceList11.get(i).get("name").toString());
            rowi.getCell(4).setCellValue(deviceList11.get(i).get("sex").toString());


        }
//      生产步骤
        int productionStepNumber = sheet.getLastRowNum() + 1;
        HSSFRow productionStepRow = sheet.createRow(productionStepNumber);
        for (int t = 0; t < colSum; t++) {
            productionStepRow.createCell(t);
        }
        sheet.addMergedRegion(new CellRangeAddress(productionStepNumber, productionStepNumber, 0, 6));
        productionStepRow.getCell(0).setCellValue("生产步骤");

        int productionStepContentNumber = sheet.getLastRowNum() + 1;
        HSSFRow productionStepContentRow = sheet.createRow(productionStepContentNumber);
        for (int t = 0; t < colSum; t++) {
            productionStepContentRow.createCell(t);
        }
        sheet.addMergedRegion(new CellRangeAddress(productionStepContentNumber, productionStepContentNumber, 0, 6));
        productionStepContentRow.getCell(0).setCellValue("？");

//      作业要点
        int workPointNumber = sheet.getLastRowNum() + 1;
        HSSFRow workPointRow = sheet.createRow(workPointNumber);
        for (int t = 0; t < colSum; t++) {
            workPointRow.createCell(t);
        }
        sheet.addMergedRegion(new CellRangeAddress(workPointNumber, workPointNumber, 0, 6));
        workPointRow.getCell(0).setCellValue("作业要点");


        int workPointContentNumber = sheet.getLastRowNum() + 1;
        HSSFRow workPointContentRow = sheet.createRow(workPointContentNumber);
        for (int t = 0; t < colSum; t++) {
            workPointContentRow.createCell(t);
        }
        sheet.addMergedRegion(new CellRangeAddress(workPointContentNumber, workPointContentNumber, 0, 6));
        workPointContentRow.getCell(0).setCellValue("？");

//      注意事项
        int attentionItemNumber = sheet.getLastRowNum() + 1;
        HSSFRow attentionItemNumberRow = sheet.createRow(attentionItemNumber);
        for (int t = 0; t < colSum; t++) {
            attentionItemNumberRow.createCell(t);
        }
        sheet.addMergedRegion(new CellRangeAddress(attentionItemNumber, attentionItemNumber, 0, 6));
        attentionItemNumberRow.getCell(0).setCellValue("注意事项");


        int attentionItemContentNumber = sheet.getLastRowNum() + 1;
        HSSFRow attentionItemContentRow = sheet.createRow(attentionItemContentNumber);
        for (int t = 0; t < colSum; t++) {
            attentionItemContentRow.createCell(t);
        }
        sheet.addMergedRegion(new CellRangeAddress(attentionItemContentNumber, attentionItemContentNumber, 0, 6));
        attentionItemContentRow.getCell(0).setCellValue("？");


//      制定日期
        int confirmDateNumber = sheet.getLastRowNum() + 1;
        HSSFRow confirmDateNumberRow = sheet.createRow(confirmDateNumber);
        for (int t = 0; t < colSum; t++) {
            confirmDateNumberRow.createCell(t);
        }
        sheet.addMergedRegion(new CellRangeAddress(confirmDateNumber, confirmDateNumber, 0, 4));
        confirmDateNumberRow.getCell(5).setCellValue("制定日期");
        confirmDateNumberRow.getCell(6).setCellValue("？");


        int allRowNumber = sheet.getLastRowNum();
        for (int i = 0; i <= allRowNumber; i++) {
            HSSFRow tmpRow = sheet.getRow(i);
            for (int i1 = 0; i1 < colSum; i1++) {
                tmpRow.getCell(i1).setCellStyle(cellStyle);
            }
        }


        // 将Excel工作簿转换成字节流
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            workbook.write(byteArrayOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close(); // 确保关闭工作簿以释放资源
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 获取字节流数组
        byte[] excelBytes = byteArrayOutputStream.toByteArray();
        return sendRequest(excelBytes);
    }

    private InputStream outputToinputStream(ByteArrayOutputStream byteArrayOutputStream) {
        return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    }



    public Object sendRequest(byte[] mergedDocumentBytes) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        form.add("file", new ByteArrayResource(mergedDocumentBytes) {
            @Override
            public String getFilename() {
                return TEMPLATE_FILE_NAME; // 设置上传的文件名
            }
        });
        form.add("subjectCode", "fanSubject01");
        form.add("userId", "fan01");
        HttpEntity<?> requestEntity = new HttpEntity<>(form, headers);
        // 发送 HTTP 请求
        ResponseEntity<ReturnResponse> responseEntity = restTemplate.postForEntity(UPLOAD_URL, requestEntity, ReturnResponse.class);
        Object content = responseEntity.getBody().getContent();
        return content;
    }

}
